package com.zzy.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.edu.entity.EduChapter;
import com.zzy.edu.entity.EduVideo;
import com.zzy.edu.entity.vo.chapter.ChapterVo;
import com.zzy.edu.entity.vo.chapter.VideoVo;
import com.zzy.edu.mapper.EduChapterMapper;
import com.zzy.edu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.edu.service.EduVideoService;
import com.zzy.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author KAZU
 * @since 2023-01-13
 */
@SuppressWarnings("all")
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
	
	@Autowired
	private EduChapterMapper chapterMapper;
	
	@Autowired
	private EduVideoService videoService;
	
	@Override
	public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
		// 1 根据课程id查询课程里面的所有章节
		QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
		wrapperChapter.eq("course_id", courseId);
		wrapperChapter.orderByAsc("sort");
		List<EduChapter> eduChapterList = chapterMapper.selectList(wrapperChapter);
		// 2 根据课程id查询所有小节
		QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
		wrapperVideo.eq("course_id", courseId);
		wrapperVideo.orderByAsc("sort");
		List<EduVideo> eduVideoList = videoService.list(wrapperVideo);
		// 3 遍历查询章节list集合进行封装
		List<ChapterVo> chapterVoList = new ArrayList<>();
		for (EduChapter eduChapter : eduChapterList) {
			// eduChapter对象值复制到ChapterVo里面
			ChapterVo chapterVo = new ChapterVo();
			BeanUtils.copyProperties(eduChapter, chapterVo);
			// 把chapterVo放到最终的list集合里面
			chapterVoList.add(chapterVo);
			// 4 遍历查询小节list集合进行封装
			// 建立章节用于封装章节的小节
			List<VideoVo> videoVoList = new ArrayList<>();
			for (EduVideo eduVideo : eduVideoList) {
				// 判断小节里面chapterid和章节里面的id是否一样
				if (eduVideo.getChapterId().equals(eduChapter.getId())) {
					// 进行封装
					VideoVo videoVo = new VideoVo();
					BeanUtils.copyProperties(eduVideo, videoVo);
					videoVoList.add(videoVo);
//					System.out.println(videoVo);
				}
			}
			// 把封装之后的小节集合，放到章节对象里面去
			chapterVo.setChildren(videoVoList);
		}
		return chapterVoList;
	}
	
	/**
	 * 删除章节
	 *
	 * @return
	 */
	@Override
	public Boolean deleteChapter(String chapterId) {
		// 根据chapterId查询小节数，如果有不删，没有就删
		QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
		wrapper.eq("chapter_id", chapterId);
		int count = videoService.count(wrapper);
		// 判断
		if (count > 0) {
			throw new GuliException(20001, "不能删除");
		} else {
			int result = chapterMapper.deleteById(chapterId);
			return result > 0;
		}
	}
	
	/**
	 * 根据课程id删除章节
	 * */
	@Override
	public void removeChapterByCourseId(String courseId) {
		QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id", courseId);
		int delete = chapterMapper.delete(wrapper);
	}
}
